package com.landiao.ctrler.management;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.ZTree;
import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.Mdse;
import com.landiao.entities.MdseStore;
import com.landiao.entities.Shop;
import com.landiao.service.MdseService;
import com.landiao.service.MdseStoreService;

/**
 * 功能说明:商品库存
 *
 */
@Controller
@RequestMapping("/management/mdse/store")
public class MdseStoreCtrler extends BaseCRUDCtrler<MdseStore, String> {

	@Autowired
	private MdseService mdseService;
	
	private MdseStoreService getMdseStoreService() {
        return (MdseStoreService) baseService;
    }
	
	@RequestMapping("index/{id}")
	public String index(Model model,@PathVariable String id) {
		log.debug("index");
		model.addAttribute("mdseId", id);
		return viewName("index");
	}
	
	@RequestMapping(value = "add/{id}", method = RequestMethod.GET)
	public String add(Model model,@PathVariable String id) {
		Mdse t = mdseService.getById(id);
		model.addAttribute("t",t);
		return viewName("add");
	}
	
	@RequestMapping(value = "save")
	public String save(MdseStore t,RedirectAttributes redirectAttr) {
		try{
			this.setEntityId(t);
			this.setCompId(t);
			baseService.save(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("添加成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("添加失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("添加失败"));
		}
		return redirectToUrl(viewName("index")+"/"+t.getMdseId());
	}
	
	@RequestMapping(value = "update")
	public String update(MdseStore t,RedirectAttributes redirectAttr) {
		try{
			baseService.update(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("修改成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("修改失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("修改失败"));
		}
		return redirectToUrl(viewName("index")+"/"+t.getMdseId());
	}
	
	@RequestMapping("noStoreShopTree/{mdseId}")
	public String noStoreShopTree(Model model,@PathVariable String mdseId) {
		String shopId = this.getClientUser().getUser().getShopId();
		List<Shop> shops = this.getMdseStoreService().getNoStoreShop(mdseId, shopId);
		List<ZTree> shopTrees = new ArrayList<>();
		if(shops!=null&&shops.size()>0){
			for(Shop shop:shops){
				ZTree ztree = new ZTree();
				ztree.setId(shop.getShopId());
				ztree.setName(shop.getName());
				ztree.setpId(shop.getPshopId());
				ztree.setOpen(true);
				
				shopTrees.add(ztree);
			}
		}
		model.addAttribute("shopTrees", shopTrees);
		return "management/shop/shopTree";
	}
}
